home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / grn / hdb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-13  |  5.9 KB  |  252 lines

  1. /*    hdb.c    1.9    (Berkeley) 86/04/14
  2.  *
  3.  * Copyright -C- 1982 Barry S. Roitblat
  4.  *
  5.  *      This file contains database routines for the hard copy programs of the
  6.  * gremlin picture editor.
  7.  */
  8.  
  9. #include "gprint.h"
  10. #include <ctype.h>
  11.  
  12. #define MAXSTRING 128
  13.  
  14. /* imports from main.c */
  15.  
  16. extern int linenum;        /* current line number in input file */
  17. extern char gremlinfile[];    /* name of file currently reading */
  18. extern int SUNFILE;        /* TRUE if SUN gremlin file */
  19.  
  20. /* imports from c */
  21.  
  22. extern char *malloc();
  23. extern char *strcpy();
  24. extern char *sprintf();
  25.  
  26. /* imports from point.c */
  27.  
  28. extern POINT *PTInit();
  29. extern POINT *PTMakePoint();
  30.  
  31. /*
  32.  * This routine returns a pointer to an initialized database element
  33.  * which would be the only element in an empty list.
  34.  */
  35. ELT *
  36. DBInit()
  37. {
  38.     return((ELT *) NULL);
  39. }  /* end DBInit */
  40.  
  41.  
  42. /*
  43.  * This routine creates a new element with the specified attributes and
  44.  * links it into database.
  45.  */
  46. ELT *
  47. DBCreateElt(type, pointlist, brush, size, text, db)
  48. int type, brush, size;
  49. POINT *pointlist;
  50. char *text;
  51. ELT *(*db);
  52. {
  53.     register ELT *temp;
  54.  
  55.     temp = (ELT *) malloc(sizeof(ELT));
  56.     temp->nextelt = *db;
  57.     temp->type = type;
  58.     temp->ptlist = pointlist;
  59.     temp->brushf = brush;
  60.     temp->size = size;
  61.     temp->textpt = text;
  62.     *db = temp;
  63.     return(temp);
  64. } /* end CreateElt */
  65.  
  66.  
  67. /*
  68.  * This routine reads the specified file into a database and 
  69.  * returns a pointer to that database.
  70.  */
  71. ELT *
  72. DBRead(file)
  73. register FILE *file;
  74. {
  75.     register int i;
  76.     register int done;        /* flag for input exhausted */
  77.     register float nx;        /* x holder so x is not set before orienting */
  78.     int type;            /* element type */
  79.     ELT *elist;            /* pointer to the file's elements */
  80.     POINT *plist;        /* pointer for reading in points */
  81.     char  string[MAXSTRING], *txt;
  82.     float x, y;            /* x and y are read in point coords */
  83.     char ch;
  84.     int len, brush, size;
  85.     int lastpoint;
  86.  
  87.  
  88.     SUNFILE = FALSE;
  89.     elist = DBInit();
  90.     (void) fscanf(file,"%s\n",string);
  91.     if (strcmp(string, "gremlinfile")) {
  92.     if (strcmp(string, "sungremlinfile")) {
  93.         error("%s is not a gremlin file", gremlinfile);
  94.         return(elist);
  95.     }
  96.     SUNFILE = TRUE;
  97.     }
  98.     (void) fscanf(file, "%d%f%f\n", &size, &x, &y);
  99.             /* ignore orientation and file positioning point */
  100.     done = FALSE;
  101.     while (!done) {
  102.         if (fgets(string, MAXSTRING, file) == NULL) {
  103.             error("%s, error in file format", gremlinfile);
  104.             return(elist);
  105.         } 
  106.     type = DBGetType(string);        /* interpret element type */
  107.         if (type < 0) {                    /* no more data */
  108.             done = TRUE;
  109.             (void) fclose(file);
  110.         } else {
  111.             (void) fscanf(file, "%f%f\n", &x, &y);    /* always one point */
  112.             plist = PTInit();                /* NULL point list */
  113.  
  114.         /* Files created on the SUN have point lists terminated 
  115.          * by a line containing only an asterik ('*').  Files 
  116.          * created on the AED have point lists terminated by the
  117.          * coordinate pair (-1.00 -1.00).
  118.          */
  119.         if (TEXT(type)) {    /* read only first point for TEXT elements */
  120.         nx = xorn(x, y);
  121.         y = yorn(x, y);
  122.                 (void) PTMakePoint(nx, y, &plist);
  123.         savebounds(nx, y);
  124.  
  125.         lastpoint = FALSE;
  126.                 do {
  127.             fgets(string, MAXSTRING, file);
  128.             if (string[0] == '*') {    /* SUN gremlin file */
  129.             lastpoint = TRUE;
  130.             }
  131.             else {
  132.             (void) sscanf(string, "%f%f", &x, &y);
  133.             if ((x == -1.00 && y == -1.00) && (!SUNFILE)) {
  134.                 lastpoint = TRUE;
  135.             } else {
  136.                 savebounds(xorn(x, y), yorn(x, y));
  137.             }
  138.             }
  139.         } while (!lastpoint);
  140.         } 
  141.         else {        /* not TEXT element */
  142.         lastpoint = FALSE;
  143.         while (!lastpoint) {
  144.             nx = xorn(x, y);
  145.             y = yorn(x, y);
  146.             (void) PTMakePoint(nx, y, &plist);
  147.             savebounds(nx, y);
  148.  
  149.             fgets(string, MAXSTRING, file);
  150.             if (string[0] == '*') {    /* SUN gremlin file */
  151.             lastpoint = TRUE;
  152.             }
  153.             else {
  154.             (void) sscanf(string, "%f%f", &x, &y);
  155.             if ((x == -1.00 && y == -1.00) && (!SUNFILE))
  156.                 lastpoint = TRUE;
  157.             }
  158.         }
  159.         }
  160.             (void) fscanf(file, "%d%d\n", &brush, &size);
  161.             (void) fscanf(file, "%d", &len);    /* text length */
  162.         ch = getc(file);            /* eat blank */
  163.             txt = malloc((unsigned) len + 1);
  164.             for (i=0; i<len; ++i) {        /* read text */
  165.                 txt[i] = getc(file);
  166.         }
  167.             txt[len] = '\0';
  168.         if (ch!='\n') fgets(string, MAXSTRING, file);    /* eat newline */
  169.             (void) DBCreateElt(type, plist, brush, size, txt, &elist);
  170.         }  /* end else */
  171.     }  /* end while not done */;
  172.     return(elist);
  173. } /* end DBRead */
  174.  
  175.  
  176. /*
  177.  * Interpret element type in string s.
  178.  * Old file format consisted of integer element types.
  179.  * New file format has literal names for element types.
  180.  */
  181. DBGetType(s)
  182. register char *s;
  183. {
  184.     while (*s==' ') s++;
  185.  
  186.     if (isdigit(s[0]) || (s[0] == '-'))        /* old element format or EOF */
  187.     return(atoi(s));
  188.  
  189.     switch (s[0]) {
  190.     case 'P':
  191.         return(POLYGON);
  192.     case 'V':
  193.         return(VECTOR);
  194.     case 'A':
  195.         return(ARC);
  196.     case 'C':
  197.         if (s[1] == 'U') {
  198.             if (s[5]  == '\n') return (CURVE);
  199.         switch (s[7]) {
  200.         case 'S': 
  201.             return(BSPLINE);
  202.         case 'E':
  203. /*
  204.             fprintf(stderr, "Warning: Bezier Curves will be printed as B-Splines\n");
  205.             return(BSPLINE);
  206. */
  207.             return(BEZIER);
  208.         default:
  209.             return(CURVE);
  210.         }
  211.         }
  212.         switch (s[4]) {
  213.         case 'L':
  214.             return(CENTLEFT);
  215.         case 'C':
  216.             return(CENTCENT);
  217.         case 'R':
  218.             return(CENTRIGHT);
  219.         default:
  220.             error("unknown element type");
  221.             exit(1);
  222.         }
  223.     case 'B':
  224.         switch (s[3]) {
  225.         case 'L':
  226.             return(BOTLEFT);
  227.         case 'C':
  228.             return(BOTCENT);
  229.         case 'R':
  230.             return(BOTRIGHT);
  231.         default:
  232.             error("unknown element type");
  233.             exit(1);
  234.         }
  235.     case 'T':
  236.         switch (s[3]) {
  237.         case 'L':
  238.             return(TOPLEFT);
  239.         case 'C':
  240.             return(TOPCENT);
  241.         case 'R':
  242.             return(TOPRIGHT);
  243.         default:
  244.             error("unknown element type");
  245.             exit(1);
  246.         }
  247.     default:
  248.         error("unknown element type");
  249.         exit(1);
  250.     }
  251. }
  252.